
//for chat application
var refinterval = '';
	
	function inputkeypress(event, theid) {
	  var theid = theid;
	 
		if (event.which == 13) {
			
			var msg = $('#'+theid).val();
			
			sayHello(msg);
		}
		var msg = $('#'+theid).val();
		if(msg.length == 1) {
			sayTyping();
			
		}
		
		clearInterval(refinterval);
		refinterval = setInterval(function(){myTimer()},8000);
				
		
	}

	function displayMessage (message) {
		// You may reviece messages that aren't chat messages. Messages that have data are chat messages
		
		if (message.data) {
			// Appends the message to a new line in the chat display
			
			var msgdata = message.data;
			var msgArr = msgdata.split('~');
			
			if (msgArr[0] == 'TYPING') {
				var senderid = msgArr[1];
				
				var theTargetChatWindow = Ext.getCmp(senderid);
				if (theTargetChatWindow) {
					var dispAlert = theTargetChatWindow.down('displayfield[name=typingtextonly]');
					dispAlert.setValue('<strong>. . .</strong>');
				}
				
			} else if (msgArr[0] == 'NOTTYPING') {
				var senderid = msgArr[1];
				
				var theTargetChatWindow = Ext.getCmp(senderid);
				if (theTargetChatWindow) {
					var dispAlert = theTargetChatWindow.down('displayfield[name=typingtextonly]');
					dispAlert.setValue('');
				}
			} else {
				var senderid = msgArr[2];
				var recipientidreal = msgArr[4];
				
				var theTargetChatWindow = Ext.getCmp(senderid);
				if (theTargetChatWindow) {
						var myPanel = theTargetChatWindow.down('panel');
						myPanel.add(new Ext.form.DisplayField({
							value: msgArr[1],
							cls: 'field-margin',
							width: '95%'
						}));
						
						var d = myPanel.body.dom;
						myPanel.body.scroll("b", d.scrollHeight);
						
						
				} else {
					//check if allowed to create new window
					var dummyid = senderid.split('^^^');
					var meAndRecipient = dummyid[1] + '^^^' + dummyid[0];
					var theTargetChatWindowB = Ext.getCmp(meAndRecipient);
				    if (theTargetChatWindowB) {
						return true;
					}
					
					var ownerUserID = Ext.get('theapplicationuserid').dom.textContent;
					console.log(ownerUserID + ' - ' + msgArr[4]);
					if(ownerUserID != msgArr[4]) {
						return true;
					}
					//open a window
					var theNewWin = Ext.create('Ext.window.Window', { 
						alias: 'widget.chatwindow',
						id: senderid,
					    title: dummyid[1],
						resizable: true,
						closable: true,
						//autoScroll: true,
						autoShow: true,
					    height: 240,
					    width: 250,
					    layout: {
							type: 'vbox',
							align: 'left'
						},
						items: [{
								xtype: 'panel',
								layout: 'vbox',
								width: '100%',
								autoScroll: true,
								flex: 15,
								items: []
							},{
								xtype: 'displayfield',
								width: '100%',
								flex: 1,
								name: 'typingtextonly'
							},{
								xtype: 'textareafield',
								enableKeyEvents: true,
								width: '100%',
								flex: 3,
								emptyText: 'Enter text here...',
								name: 'responseonchat',
								listeners: {
									keyup: function(thiscomp,e) {
										var thevaluea = thiscomp.value;
										if(e.button == 12) {
											
											if (thevaluea.trim() == "") {
												return false;
											}
											var timenow = new Date();
											timenow = Ext.Date.format(timenow, "h:i:s a");
											thevalue = '<strong>Me: </strong>' + thevaluea + '<i>(' + timenow + ')</i>';
											var thiswin = this.up('window');
											
											
											//displayf.setValue(thevalue);
											thiscomp.setValue("");
											var myPanel = thiswin.down('panel');
											myPanel.add(new Ext.form.DisplayField({
						                            value: thevalue,
													cls: 'field-margin',
													width: '95%'
						                        }));
											
											var d = myPanel.body.dom;
											myPanel.body.scroll("b", d.scrollHeight);
											
											var winid = thiswin.id.split('^^^');
											var recipientAndMe = winid[1] + '^^^' + winid[0];
											
											var theFullMsg = '<strong>' + winid[0] + ': </strong>' + thevaluea + '<i>(' + timenow + ')</i>';
											sayHello(theFullMsg, recipientAndMe, winid[0], thiswin.title);
										} else {
											var thiswin = this.up('window');
											var winid = thiswin.id.split('^^^');
											var recipientAndMe = winid[1] + '^^^' + winid[0];
											
											if((thevaluea.length%9) == 0 || thevaluea.length == 1) {
												sayTyping(recipientAndMe, winid[0], thiswin.title);
											}
											window.setTimeout(function(){myTimer(recipientAndMe)},15000);
										}
										
								    }
								}
							}]
						
					});
					var myPanel = theNewWin.down('panel');
					myPanel.add(new Ext.form.DisplayField({
							value: msgArr[1],
							cls: 'field-margin',
							width: '95%'
						}));
					
					
					
				}
				
			}
			
		}

		
	
	}

	function sayHello (msg,myuserid, myname, recipientuserid) {
		// Gets the message from the message box
		var msg = msg;
		if (msg.length < 1) {
			return false;
		}
		
		
		
		var d = new Date();
		var hours = d.getHours(); 
		var minutes = d.getMinutes(); 
		var hours = hours+':'+minutes;
		
		var youruserid = myuserid;
		var myname     = myname;
		var recipientid = recipientuserid; 
		
		msg = 'SAYHELLO~' + msg + '~' + youruserid + '~' + myname +'~' + recipientid; 
        
		// Publishes the message to everyone who is subscribed to the chat channel
		ws.publish("chat", msg);

		// Clears the message just sent 
		//$('#'+youruserid + 'message').val('');
		//$('.messageempty').val('');

		// Moves cursor focus to the message box
		//$('#'+youruserid + 'message').focus();
		
		
	}
	
	function sayTyping(recipientAndMe) {
		var youruserid = recipientAndMe;
		
		msg = 'TYPING~'+youruserid; 


		// Publishes the message to everyone who is subscribed to the chat channel
		ws.publish("chat", msg);
		
	}
	
	function displayError(message){
		console.log('iBOS/e has encountered an error.');
	}
	function displayOpen(message){
		//alert('On open');
	}
	function displayClose(message){
		//alert('On close ');
	}
	
	function myTimer(recipientAndMe){
		var youruserid = recipientAndMe;
		
		msg = 'NOTTYPING~'+youruserid; 
		clearInterval(this);

		// Publishes the message to everyone who is subscribed to the chat channel
		ws.publish("chat", msg);
		
									
		
	}
	
	function inputclicks(heruserid) {
		var heruserid = heruserid;
		console.log(heruserid);
		$('#currentdialoguser').html(heruserid);
	}


    function getdialog(heruserid, hername, myuserid, myname) {
		
		
	}
	
//end for chat application